package org.komarichin.tiles;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;
import org.komarichin.Constants;
import org.komarichin.struts2.BaseAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * User: Mykhaylo Komarichyn
 * Date: 19.01.2010
 * Time: 10:38:22
 */
public class SeoPreparer implements ViewPreparer, TextProvider, LocaleProvider {

    //<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">

    private static final Logger log = LoggerFactory.getLogger(SeoPreparer.class);

    private final transient TextProvider textProvider = new TextProviderFactory().createInstance(getClass(), this);

    private void setValue(Attribute attr, String val, String key) {
        if (attr != null) {
            if (StringUtils.isEmpty(val)) {
                val = getText(attr.getValue().toString());
            }
        }
        HttpServletRequest req = ServletActionContext.getRequest();
        req.setAttribute(key, StringUtils.defaultIfEmpty(val, StringUtils.EMPTY));
    }


    @Override
    public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) {
        Object action = ActionContext.getContext().getValueStack().getContext().get("action");
        if (action instanceof BaseAction) {
            BaseAction act = (BaseAction) action;
            setValue(attributeContext.getAttribute(Constants.TITLE), act.getPageTitle(), Constants.TITLE);
            setValue(attributeContext.getAttribute(Constants.DESCRIPTION), act.getPageDescription(), Constants.DESCRIPTION);
            setValue(attributeContext.getAttribute(Constants.KEY_WORDS), act.getPageKeyWords(), Constants.KEY_WORDS);
            setValue(attributeContext.getAttribute(Constants.EXP_DATE), act.getPageExpire(), Constants.EXP_DATE);
        }

    }


    public Locale getLocale() {
        ActionContext ctx = ActionContext.getContext();
        if (ctx != null) {
            return ctx.getLocale();
        } else {
            log.debug("Action context not initialized");
            return null;
        }
    }

    public boolean hasKey(String key) {
        return textProvider.hasKey(key);
    }

    public String getText(String aTextName) {
        return textProvider.getText(aTextName);
    }

    public String getText(String aTextName, String defaultValue) {
        return textProvider.getText(aTextName, defaultValue);
    }

    public String getText(String aTextName, String defaultValue, String obj) {
        return textProvider.getText(aTextName, defaultValue, obj);
    }

    public String getText(String aTextName, List<Object> args) {
        return textProvider.getText(aTextName, args);
    }

    public String getText(String key, String[] args) {
        return textProvider.getText(key, args);
    }

    public String getText(String aTextName, String defaultValue, List<Object> args) {
        return textProvider.getText(aTextName, defaultValue, args);
    }

    public String getText(String key, String defaultValue, String[] args) {
        return textProvider.getText(key, defaultValue, args);
    }

    public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {
        return textProvider.getText(key, defaultValue, args, stack);
    }

    public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
        return textProvider.getText(key, defaultValue, args, stack);
    }

    public ResourceBundle getTexts() {
        return textProvider.getTexts();
    }

    public ResourceBundle getTexts(String aBundleName) {
        return textProvider.getTexts(aBundleName);
    }

}
